package com.mobile.challenge.ui.report.unit.admin

import android.app.DatePickerDialog
import android.os.Bundle
import androidx.lifecycle.Observer
import com.google.android.material.tabs.TabLayout
import com.mobile.challenge.R
import com.mobile.challenge.databinding.FragmentUnitReportBinding
import com.mobile.challenge.ui.report.model.PeriodType
import com.mobile.challenge.ui.report.unit.admin.adapter.AdminMoreFragmentAdapter
import com.mobile.challenge.ui.report.unit.admin.adapter.AdminOneFragmentAdapter
import com.mobile.challenge.ui.report.utils.ReportPagerUtils
import com.mobile.frame.mvvmframe.base.BaseFragment
import java.text.SimpleDateFormat
import java.util.*

/**
 * @Author Touge
 * @Date 2020/9/15 22:37
 * @Description
 */
class AdminUnitReportFragment : BaseFragment<AdminUnitReportViewModel, FragmentUnitReportBinding>(),
        TabLayout.OnTabSelectedListener {

    companion object {
        fun newInstance(): AdminUnitReportFragment {
            val args = Bundle()

            val fragment = AdminUnitReportFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var currentCalendar = Calendar.getInstance()
    private var periodType = PeriodType.DAILY
    private lateinit var mManagerOneFragmentAdapter: AdminOneFragmentAdapter
    private lateinit var mManagerMoreFragmentAdapter: AdminMoreFragmentAdapter
    private lateinit var unitList: ArrayList<String>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mManagerOneFragmentAdapter = AdminOneFragmentAdapter(this)
        mManagerMoreFragmentAdapter = AdminMoreFragmentAdapter(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_unit_report
    }

    override fun initData(savedInstanceState: Bundle?) {

        mViewModel.dataList.observe(this, Observer { data ->
            mManagerMoreFragmentAdapter.fragmentList = ReportPagerUtils.getHistogramFragmentList(data)
        })

        // 初始化当前时间
        mBinding.currentTimeText.setOnClickListener {
            showDatePickDialog()
        }
        updateText()

        // 注册周期选择监听
        mBinding.periodTabLayout.addOnTabSelectedListener(this)
        updateData()
    }

    /**
     * 显示时间选择器
     */
    private fun showDatePickDialog() {
        val datePickerDialog = DatePickerDialog(
                requireContext(),
                DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
                    currentCalendar = Calendar.getInstance().apply {
                        set(year, month, dayOfMonth, 0, 0, 0)
                    }
                    updateText()
                    updateData()
                },
                currentCalendar.get(Calendar.YEAR),
                currentCalendar.get(Calendar.MONTH),
                currentCalendar.get(Calendar.DAY_OF_MONTH)
        )
        val datePicker = datePickerDialog.datePicker
        datePicker.maxDate = System.currentTimeMillis()
        try {
            datePickerDialog.show()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 更新文字
     */
    private fun updateText() {
        mBinding.currentTimeText.text = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
                .format(currentCalendar.timeInMillis)
    }

    override fun onTabReselected(tab: TabLayout.Tab?) {

    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {

    }

    override fun onTabSelected(tab: TabLayout.Tab?) {
        periodType = when (tab?.position) {
            0 -> {
                PeriodType.DAILY
            }
            1 -> {
                PeriodType.WEEKLY

            }
            else -> {
                PeriodType.MONTH
            }
        }
        updateData()
    }

    private fun updateData() {
        when (periodType) {
            PeriodType.DAILY -> {
                mBinding.unitReportViewPager.adapter = mManagerOneFragmentAdapter
                mManagerOneFragmentAdapter.refresh(currentCalendar, periodType)
            }
            else -> {
                mBinding.unitReportViewPager.adapter = mManagerMoreFragmentAdapter
                mViewModel.getAdminData(currentCalendar, periodType)
                mManagerMoreFragmentAdapter.refresh(currentCalendar, periodType)
            }
        }
    }
}